<?php $this->load->view('intro'); ?>
<body id="item_relation">
<?php $this->load->view('admin/navigation'); ?>
<div id="edit_section">

<label>Food Category: </label>
<select name="food_category_id" id="food_category_group" onkeyup="update_selection()" onchange="update_selection()">
<?php foreach($food_category as $food_cat): ?>
<option
<?php if($food_category_select == $food_cat['id']) echo "selected=\"selected\""; ?>
value="<?=$food_cat['id']?>"><?=$food_cat['en_name']?></option>
<?php endforeach; ?>
</select>

<label>Food Item: </label>
<select name="food_item_id" id="food_item_group" onkeyup="update_selection()" onchange="update_selection()">
<?php foreach($food_items as $food): ?>
<option
<?php if($food_select == $food['id']) echo "selected=\"selected\""; ?>
value="<?=$food['id']?>"><?=$food['en_name']?></option>
<?php endforeach; ?>
</select>
<br/><br/>

<label>Food Option Category: </label>
<select name="food_optoin_category_id" id="food_option_category_group" onkeyup="update_selection()" onchange="update_selection()">
<?php foreach($food_option_category as $option_category): ?>
<option
<?php if($opt_select == $option_category['id']) echo "selected=\"selected\""; ?>
value="<?=$option_category['id']?>"><?=$option_category['en_name']?></option>
<?php endforeach; ?>
</select>
<br/><br/>

<fieldset>
    <?php foreach($food_option_items as $food_option_item): ?>
    <input type="checkbox" value="<?=$food_option_item['id']?>" onchange="change_relation(this.checked, this.value)"
    <?php if(in_array($food_option_item['id'], $food_item_relations)) echo 'checked = "checked"';?>
    >&nbsp;&nbsp;<label><?=$food_option_item['en_name']?></label><br/>
    <?php endforeach; ?>
</fieldset>

<form method="post" id="helper_form" action="index.php/admin/item_relation">
    <input name="old_food_category" id="old_food_category" type="hidden" value="<?=$food_category_select?>" />
    <input name="food_select" id="food_select" type="hidden" value="" />
    <input name="food_category_select" id="food_category_select" type="hidden" value="" />
    <input name="opt_select" id="opt_select" type="hidden" value="" />
</form>

<script type="text/javascript">

function update_selection()
{
    var $old_category = $("#old_food_category").val();
    var $food_category = $("#food_category_group").val();
    var $food_item = $("#food_item_group").val();
    var $food_opt = $("#food_option_category_group").val();
    $("#food_category_select").val($food_category);
    if($old_category == $food_category){
        $("#food_select").val($food_item);
        $("#opt_select").val($food_opt);
    }
    $("#helper_form").submit();
}

function change_relation(selection, value)
{
    var $action;
    if(selection)
        $action = 'add';
    else
        $action = 'remove';

    var $food_item = $("#food_item_group").val();

    var $data = {action:  $action, food_item: $food_item, option_item: value};
      $.ajax({
        type: "POST",
        url: "index.php/admin/item_relation/change_relation",
        dataType: "json",
        data: $data,
        success: function(msg){
            //donothing
        }
      });
}

</script>

</div>
<?php $this->load->view('outro'); ?>